package com.blork.rag;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * This is an example of implement an {@link BroadcastReceiver} for an alarm that
 * should occur once.
 */


public class SetAlarms extends BroadcastReceiver
{
	
    @Override
    public void onReceive(Context context, Intent rIntent)
    {
	  	SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
	  	
		String notificationFreq = settings.getString("notification_freq", "Every hour");
       
		AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, EventService.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        
		//If they've turned updates off, stop them, else begin checking 
		if(notificationFreq.equals("Never")){
			Log.i(Rag.TAG, "Cancelling updates");
			 am.cancel(sender);
		}else{
			Long repeat = AlarmManager.INTERVAL_HOUR; 
			if(notificationFreq.equals("Once a day")){
				repeat = AlarmManager.INTERVAL_DAY;
			}else if(notificationFreq.equals("Once a week")){
				repeat = 7*AlarmManager.INTERVAL_DAY;
			}

            // Schedule the alarm!
            long firstTime = SystemClock.elapsedRealtime();
            
            am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
                    firstTime+repeat, repeat, sender);
            
            Log.i(Rag.TAG, "Set updates for "+notificationFreq);
		}
    }
   
}

